﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLL;
using Model;
using Web.Helper;

namespace Web.Areas.Manager.Controllers
{
  // [UserAuthorize]
    public class StaffPerformanceController : Controller
    {
      // StaffPerformance_BLL _StaffPerformance_BLL = new StaffPerformance_BLL();
        Sys_Users_BLL _Sys_Users_BLL = new Sys_Users_BLL();
        Customer_BLL _Customer_BLL = new Customer_BLL();
        Quotation_BLL _Quotation_BLL = new Quotation_BLL();
        SalesOpportunity_BLL _SalesOpportunity_BLL = new SalesOpportunity_BLL();
        ContactOrder_BLL _ContactOrder_BLL=new ContactOrder_BLL ();
      //  ReceiveForm_BLL _ReceiveForm_BLL = new ReceiveForm_BLL();
        M_ReceiveForm_BLL _M_ReceiveForm_BLL = new M_ReceiveForm_BLL();
        public virtual ActionResult Index(int? pageIndex, string keyword, DateTime? fromtime, DateTime? totime)
        {
            var model = _Sys_Users_BLL.Get();
            if (!string.IsNullOrEmpty(keyword))
            {
             model = model.Where(a => a.UserName.Contains(keyword));
            }

           
            if (fromtime != null && totime != null)
            {
                var end = totime.Value.AddDays(1);
                    ViewData["from"]=fromtime;
                    ViewData["end"]=end;
              
            }
            return View(model.ToPagedList(pageIndex));
        }


        public virtual ActionResult Details(Guid Id)
        {
            var item = _Sys_Users_BLL.Get(Id);
            return View(item);
        }

        public ActionResult Customer(int? pageIndex, DateTime? fromtime, DateTime? totime, string keyword)
        {
            var model = _Customer_BLL.Get();

            if (!string.IsNullOrEmpty(keyword))
            { 
              model=model.Where (a=>a.Sys_Users.UserName.Equals(keyword));
            }

            if (fromtime != null && totime != null)
            {
                var end = totime.Value.AddDays(1);
                model = model.Where(a => a.AddDate >= fromtime.Value && a.AddDate <= end );
            }
            return View(model.ToPagedList(pageIndex));
        }

        public ActionResult Quotation(int? pageIndex, DateTime? fromtime, DateTime? totime, string keyword)
        {
            var model =_Quotation_BLL.Get();
            if (!string.IsNullOrEmpty(keyword))
            {
                model = model.Where(a => a.Sys_Users.UserName.Equals(keyword));
            }
            if (fromtime != null && totime != null)
            {
                var end = totime.Value.AddDays(1);
                model = model.Where(a => a.DateTime >= fromtime.Value && a.DateTime <= end);
            }
            return View(model.ToPagedList(pageIndex));
        }


        public ActionResult SalesOpportunity(int? pageIndex, DateTime? fromtime, DateTime? totime, string keyword)
        {
            var model = _SalesOpportunity_BLL.Get();
            if (!string.IsNullOrEmpty(keyword))
            {
                model = model.Where(a => a.Sys_Users.UserName.Equals(keyword));
            }
            if (fromtime != null && totime != null)
            {
                var end = totime.Value.AddDays(1);
                model = model.Where(a => a.DateTime >= fromtime.Value && a.DateTime <= end);
            }
            return View(model.ToPagedList(pageIndex));
        }

        public ActionResult Contact(int? pageIndex, DateTime? fromtime, DateTime? totime,string keyword)
        {
            var model = _ContactOrder_BLL.Get();
            if (!string.IsNullOrEmpty(keyword))
            {
                model = model.Where(a => a.Sys_Users.UserName.Equals(keyword));
            }
            if (fromtime != null && totime != null)
            {
                var end = totime.Value.AddDays(1);
                model = model.Where(a => a.DateTime >= fromtime.Value && a.DateTime <= end);
            }
            return View(model.ToPagedList(pageIndex));
        }

        public ActionResult Receive(int? pageIndex, DateTime? fromtime, DateTime? totime,string keyword)
        {
            var model = _ContactOrder_BLL.Get();
            if (!string.IsNullOrEmpty(keyword))
            {
                model = model.Where(a => a.Sys_Users.UserName.Equals(keyword));
            }
            if (fromtime != null && totime != null)
            {
                var end = totime.Value.AddDays(1);
                model = model.Where(a => a.DateTime  >= fromtime.Value && a.DateTime <= end);
            }
            return View(model.ToPagedList(pageIndex));
        }

        public virtual ActionResult Edit(Guid? Id)
        {
            var item = _Sys_Users_BLL.Get(Id);
            return View(item);
        }

        [HttpPost]
         public virtual ActionResult Edit(Guid? Id, Sys_Users collection)
        {
            if (!ModelState.IsValid)
            {
                return View(collection);
            }
            _Sys_Users_BLL.Save(Id, ref collection);
            return RedirectToAction("Index",MyrouteValues.Get());
        }

        [HttpDelete]
        public virtual ActionResult Delete(Guid Id)
        {
            _Sys_Users_BLL.DeleteObject(Id);
            return RedirectToAction("Index",MyrouteValues.Get());
        }
    }
}
